﻿#pragma once

#include <QTreeWidget>
#include <QFileInfo>
#include <QFileIconProvider>
#include "UIUtilsExport.h"

namespace Domy {

class FileSystemItem;
class FileSystemDisplayProvider;

/**
 * @brief The FileSystemTreeWidget class
 * @note  文件系统树状图
 */
class UIUTILS_EXPORT FileSystemTreeWidget : public QTreeWidget
{
    Q_OBJECT
public:
    explicit FileSystemTreeWidget(QWidget *parent = Q_NULLPTR);
    ~FileSystemTreeWidget();

    /**
     * @brief add
     * @note  添加
     * @param fi 文件信息
     */
    void add(const QFileInfo &fi);

    /**
     * @brief add
     * @note  添加
     * @param fi_list 文件信息列表
     */
    void add(const QFileInfoList &fi_list);

    /**
     * @brief 设置表头
     */
    void setHeaderContent(const QString &text, const QIcon &icon);
    void setHeaderText(const QString &text);
    void setHeaderIcon(const QIcon &icon);

signals:
    void signalOpenFile(const QString &file_path);

private slots:
    void slotOnItemExpanded(QTreeWidgetItem *item);
    void slotOnItemCollapsed(QTreeWidgetItem *item);
    void slotOnItemDoubleClicked(QTreeWidgetItem *item, int column);
    void slotOnItemChanged(QTreeWidgetItem *item, int column);
    void slotOnCustomContextMenuRequested(const QPoint &pos);

private:
    QTreeWidgetItem *getHeaderItem();

    void doAction(FileSystemItem *item, int action);

    void addBegin();
    void addFinished();

private:
    int m_current_action;
    FileSystemDisplayProvider *m_provider;
};

/**
 * @brief The FileSystemDisplayProvider class
 * @note  文件系统图标提供器
 */
class FileSystemDisplayProvider : public QFileIconProvider
{
public:
    FileSystemDisplayProvider() {}
    ~FileSystemDisplayProvider() {}

    QPair<QString, QIcon> getDisplayInfo(const QFileInfo &fi, int state = 0) const;
};

/**
 * @brief The FileSystemMenuManager class
 * @note  文件系统菜单管理
 */
class FileSystemMenuManager
{
    FileSystemMenuManager() = delete;
    ~FileSystemMenuManager() = delete;
public:

    enum FileSystemAction {
        FileSystem_None = 0,

        FileAct_Open,
        FileAct_Rename,
        FileAct_Remove,
        FileAct_ShowInExplorer,

        DirAct_AddNewFile,
        DirAct_AddNewDir,
        DirAct_Rename,
        DirAct_Remove,
        DirAct_Expand,
        DirAct_Collapse,
        DirAct_ShowInExplorer,

        RootAct_Expand,
        RootAct_Collapse,
    };

    static FileSystemAction execDirMenu(FileSystemItem *item);

    static FileSystemAction execFileMenu(FileSystemItem *item);

    static FileSystemAction execRootMenu(FileSystemItem *item);
};

} // namespace Domy
